package com.fanrui.code.chess.mahjong;

import java.util.Collection;
import java.util.List;

import com.fanrui.proto.cmd.EveryHuType;
import com.fanrui.proto.cmd.EveryHuType.IconmeType;
import com.fanrui.proto.cmd.HuType;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class MahjongIncome {

	@SerializedName("id")
	private final long scoure;
	private final MahjongIncomeInfo[] selated;
	@SerializedName("gold")
	private final int income;
	private final IconmeType iconmeType;

	private final List<HuType> huTypes;

	public MahjongIncome(long scoure, int income, IconmeType iconmeType, List<HuType> huTypes,
			MahjongIncomeInfo... selated) {
		this.scoure = scoure;
		this.income = income;
		this.selated = selated;
		this.huTypes = huTypes;
		this.iconmeType = iconmeType;
	}

	public MahjongIncome(long scoure, int income, IconmeType iconmeType, List<HuType> huTypes,
			Collection<MahjongIncomeInfo> selated) {
		this.scoure = scoure;
		this.income = income;
		this.selated = new MahjongIncomeInfo[selated.size()];
		this.huTypes = huTypes;
		if (selated != null) {
			selated.toArray(this.selated);
		}

		this.iconmeType = iconmeType;

	}

	public long getScoure() {
		return scoure;
	}

	public MahjongIncomeInfo[] getSelated() {
		return selated;
	}

	public int getIncome() {
		return income;
	}

	public List<HuType> getHuTypes() {
		return huTypes;
	}

	public IconmeType getIconmeType() {
		return iconmeType;
	}

	public EveryHuType toCmd() {
		EveryHuType.Builder builder = EveryHuType.newBuilder();
		builder.setIncome(income);
		if (huTypes != null) {
			builder.addAllType(huTypes);
		}
		if (selated != null) {
			for (MahjongIncomeInfo info : selated) {
				builder.addSelatedId(info.getPlayerId());
			}
		}

		if (iconmeType != null) {
			builder.setIconmeType(iconmeType);
		}
		return builder.build();
	}

	public static String toJsonString(Collection<MahjongIncome> incomes) {
		Gson gson = new Gson();
		return gson.toJson(incomes);
	}

	/**
	 * @ClassName: MahjongIncomeInfo
	 * @Description: 收入详细信息
	 * @author bear
	 */
	public static class MahjongIncomeInfo {
		private final long playerId;
		private final int income;

		public MahjongIncomeInfo(long playerId, int income) {
			this.playerId = playerId;
			this.income = income;
		}

		public long getPlayerId() {
			return playerId;
		}

		public int getIncome() {
			return income;
		}

		public static MahjongIncomeInfo build(long playerId, int income) {
			return new MahjongIncomeInfo(playerId, income);
		}

	}
}
